package edu.gdin.ecommerce.model;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import edu.gdin.ecommerce.util.DicConverter;

/**
 * User entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "t_user", catalog = "ecommerce")
public class User implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = -1489759818439067755L;
	private String account;
	private String password;
	private String username;
	private Integer status; // 1=正常 0=停用
	private String mobile;
	private String remarks;
	private Date lastTime;
	private List<Role> roles = new ArrayList<Role>();

	// Constructors

	/** default constructor */
	public User() {
	}

	/** full constructor */
	public User(String account, String password, String username,
			Integer status, String mobile, String remarks, Date lastTime) {
		this.account = account;
		this.password = password;
		this.username = username;
		this.status = status;
		this.mobile = mobile;
		this.remarks = remarks;
		this.lastTime = lastTime;
	}

	// Property accessors
	@Id
	@Column(name = "ACCOUNT", unique = true, nullable = false, length = 20)
	public String getAccount() {
		return this.account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	@Column(name = "PASSWORD", nullable = false, length = 20)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "USERNAME", nullable = false, length = 50)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "STATUS", nullable = false)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Column(name = "MOBILE", nullable = false, length = 20)
	public String getMobile() {
		return this.mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name = "REMARKS", length = 100)
	public String getRemarks() {
		return this.remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@Column(name = "LAST_TIME", nullable = false, length = 19)
	public Date getLastTime() {
		return this.lastTime;
	}

	public void setLastTime(Date lastTime) {
		this.lastTime = lastTime;
	}

	@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	@JoinTable(name = "t_user_role", 
			joinColumns = {@JoinColumn(name = "USER_ACCOUNT")},  
			inverseJoinColumns = {@JoinColumn(name = "ROLE_ID")})
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	
	// -- property Converter --
	
	public String statusToString() {
		return DicConverter.dicMapConvert("DIC_USER_STATUS", this.getStatus());
	}
	
	public String lastTimeToString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(this.getLastTime());
	}
	
}